package cas.ihep;

import cas.ihep.spark.HepEvent;

import java.io.*;

public class Main {

    static void write()throws Exception{
        HepEvent evt= new HepEvent();
        evt.setPhysicalSize(20);
        evt.setPackedSize(20);
        evt.setNumberOfEvents((short)10);
        evt.setPackingAlgorithm((short)0);
        evt.setVersion(0x00060505);
        System.out.println("Set basic value");
        byte[] arr=new byte[20];
        for (int i=0;i<20;++i){
            arr[i]=(byte)i;
        }
        ByteArrayInputStream bis=new ByteArrayInputStream(arr);
        DataInputStream dis=new DataInputStream(bis);
        evt.setEventData(dis);
        System.out.println("Set event data");
        FileOutputStream fos=new FileOutputStream("/home/gesturewei/test.bin");
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        oos.writeObject(evt);
        oos.close();
        fos.close();
        dis.close();
        bis.close();
        evt=null;
    }

    static void readNew(String name)throws Exception{
        HepEvent evt=new HepEvent(name);
        System.out.println("Physical size: "+evt.getPhysicalSize());
        System.out.println("Packed size: "+evt.getPackedSize());
        System.out.println("Number of events: "+evt.getNumberOfEvents());
        System.out.println("Packing algorithm: "+evt.getPackingAlgorithm());
        System.out.println("Version: "+Integer.toHexString(evt.getVersion()));
        System.out.print("Event data: ");
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        DataOutputStream oos=new DataOutputStream(bos);
        evt.getEventData(oos);
        byte[] arr=bos.toByteArray();
        for (byte b:arr){
            System.out.print(b);
            System.out.print(' ');
        }
        System.out.println();
        evt=null;
        oos.close();
        bos.close();
    }

    static void read()throws Exception {
        FileInputStream fis=new FileInputStream("/home/gesturewei/test.bin");
        ObjectInputStream ois=new ObjectInputStream(fis);
        Object obj=ois.readObject();
        System.out.println(obj.getClass());
        if (obj instanceof HepEvent){
            HepEvent evt=(HepEvent)obj;
            System.out.println("Physical size: "+evt.getPhysicalSize());
            System.out.println("Packed size: "+evt.getPackedSize());
            System.out.println("Number of events: "+evt.getNumberOfEvents());
            System.out.println("Packing algorithm: "+evt.getPackingAlgorithm());
            System.out.println("Version: "+Integer.toHexString(evt.getVersion()));
            System.out.print("Event data: ");
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            DataOutputStream oos=new DataOutputStream(bos);
            evt.getEventData(oos);
            byte[] arr=bos.toByteArray();
            for (byte b:arr){
                System.out.print(b);
                System.out.print(' ');
            }
            System.out.println();
            evt.unlink();
            evt=null;
            obj=null;
            oos.close();
            bos.close();
        }
    }

    public static void main(String[] args) throws Exception {
        //readNew("sparkhep-3821-157a7381165-37b3e1c9");
        read();
    }
}
